/*
    Copyright 2011 - Tommi Laukkanen (www.substanceofcode.com)

    This file is part of NewsFlow.

    NewsFlow is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    NewsFlow is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with NewsFlow. If not, see <http://www.gnu.org/licenses/>.
*/

import Qt 4.7

Item {
    id: newsList
    property ListModel newsTitlesModel
    property double iconOpacity: 0
    signal clicked( int index )
    signal back()
    signal reloadNews()

    Rectangle {
        anchors.fill: parent
        color: theme.backGroundColor

        ListView {
            id: newsTitlesList
            model: newsList.newsTitlesModel
            anchors.fill: parent
            delegate: newsListDelegate
            highlightFollowsCurrentItem: true
            focus: true
        }

        Image {
            visible: !theme.isWhite()
            source: "../pics/bottom-shadow.png"
            height: 64
            width: parent.width
            y: parent.height - 64
        }
/*
        Image {
            visible: !theme.isTablet
            x: 8
            y: parent.height - 56
            source: theme.isWhite() ? "../pics/tablet/on-off.png" : "../pics/on-off.png"
            width: 48
            height: 48
            opacity: newsList.iconOpacity

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    Qt.quit();
                }
            }
        }
*/
        Image {
            visible: !theme.isTablet
            x: 8 //anchors.horizontalCenter: parent.horizontalCenter
            y: parent.height - 56
            source: theme.isWhite() ? "../pics/tablet/rnd_br_first.png" : "../pics/rnd_br_first.png"
            width: 48
            height: 48
            opacity: newsList.iconOpacity

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    newsList.back();
                }
            }
        }

        Image {
            x: parent.width - 56
            y: parent.height - 56
            source: theme.isWhite() ? "../pics/tablet/playback_reload.png" : "../pics/playback_reload.png"
            width: 48
            height: 48
            opacity: newsList.iconOpacity

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    newsList.reloadNews();
                }
            }
        }
    }

    Component {
        id: newsListDelegate

        Item {
            id: newsTitleItem
            width: parent.width
            height: titleContainer.height + 8

            Rectangle {
                id: titleContainer
                color: mouseArea.pressed ? theme.menuActiveBGColor : theme.menuInactiveBGColor
                y: 2
                width: parent.width
                height: newsTitle.height + sourceLabel.height + 10

                Text {
                    id: newsTitle
                    color: theme.menuTextColor
                    opacity: !isRead ? 1 : 0.5
                    y: 5
                    x: 4
                    font.pixelSize: theme.menuFontSize
                    width: parent.width - 8
                    text: "<span>" + title + "</span>"
                    wrapMode: Text.Wrap
                }

                Text {
                    id: sourceLabel
                    color: theme.menuSubTextColor
                    y: newsTitle.y + newsTitle.height + 2
                    x: 4
                    font.pixelSize: theme.menuSubFontSize
                    width: parent.width - 100
                    text: source
                    wrapMode: Text.Wrap
                }

                Text {
                    id: authorName
                    color: theme.menuSubTextColor
                    y: newsTitle.y + newsTitle.height + 2
                    x: 4
                    font.pixelSize: theme.menuSubFontSize
                    width: parent.width - 8
                    text: published
                    wrapMode: Text.Wrap
                    horizontalAlignment: Text.AlignRight
                }
            }

            Rectangle {
                width:  parent.width - 8
                x: 4
                y: newsTitleItem.height - 2
                height: 1
                color: theme.menuSeparator
            }

            MouseArea {
                id: mouseArea
                anchors.fill: parent
                onClicked: {
                    newsList.clicked( index );
                }
            }

        }
    }
}


